﻿using D.B.Data.API.Applications.Command.RoleCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.RoleCommandHandler
{
    public class DelRoleCommandHandler : IRequestHandler<DelRoleCommand, APIResult<object>>
    {
        private readonly IBaseRepository<RoleModel> roleRepository;

        public DelRoleCommandHandler(IBaseRepository<RoleModel> roleRepository)
        {
            this.roleRepository = roleRepository;
        }

        public Task<APIResult<object>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            var res = roleRepository.GetAll().FirstOrDefault(s => s.Id == request.Id);
           res.IsDel= true;
            int count=roleRepository.Update(res);
            APIResult<object> aPIResult = new APIResult<object>();
            if(count>0)
            {
                aPIResult.Data = count;
                aPIResult.Code=EnumCode.Ok;
                aPIResult.Msg = "删除成功";
                return Task.FromResult(aPIResult);
            }
            else
            {
                aPIResult.Data = count;
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "删除失败";
                return Task.FromResult(aPIResult);
            }






        }
    }
}
